package com.crk.shixiguanli.service.impl;

import com.crk.shixiguanli.dao.*;
import com.crk.shixiguanli.entity.*;
import com.crk.shixiguanli.service.TaskService;
import com.crk.shixiguanli.util.Page;
import com.crk.shixiguanli.util.ResponseResult;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author
 * @description:
 * @date 2020/4/27
 */
@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    TaskDao taskDao;
    @Autowired
    UserDao userDao;
    @Autowired
    JobDao jobDao;
    @Autowired
    JobStudentDao jobStudentDao;
    @Autowired
    TaskCompleteDao taskCompleteDao;
    @Autowired
    StudentDao studentDao;
    /**
     * 分页查询
     *
     * @param page
     * @return
     */
    @Override
    public ResponseResult queryPage(Page page) {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        Integer count = taskDao.countByCondition(userinfo.getId());
        page.setAllRow(count);
        List<Task> planList  = taskDao.selectByCondition(userinfo.getId(),page.getStart(), page.getSize());
        List<TaskVo> result = new ArrayList<>();
        for (Task task : planList) {
            TaskVo taskVo = new TaskVo();
            taskVo.setId(task.getId());
            taskVo.setTaskName(task.getTaskName());
            taskVo.setTaskDesc(task.getTaskDesc());
            taskVo.setStartTime(task.getStartTime());
            taskVo.setEndTime(task.getEndTime());
            taskVo.setJobId(task.getJobId());
            taskVo.setJobStr(jobDao.selectByPrimaryKey(task.getJobId()).getJobName());
            taskVo.setUserName(userDao.selectByPrimaryKey(task.getUpdater()).getUserName());
            taskVo.setUpdateTime(task.getUpdateTime());

            result.add(taskVo);
        }
        return ResponseResult.success(result);
    }

    @Override
    public ResponseResult queryTaskComplete(String taskName, String status,String studentName, Page page) {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        if (userinfo.getUserType().equals("company")){
            Integer count = taskCompleteDao.countTaskCompletePage(taskName,status,studentName,userinfo.getId());
            page.setAllRow(count);
            List<TaskCompleteVo> taskCompletes = taskCompleteDao.selectTaskCompletePage(taskName,status,studentName,userinfo.getId(),page.getStart(),page.getSize());
            for (TaskCompleteVo taskComplete : taskCompletes) {
                User user = userDao.selectByPrimaryKey(taskComplete.getStudentId());
                Student studentByIdCard = studentDao.getStudentByIdCard(user.getIdCard());
                taskComplete.setUserName(studentByIdCard.getStudentName());
            }
            return ResponseResult.success(taskCompletes);
        }else{
            Integer count = taskCompleteDao.countTaskCompletePage(taskName,status,studentName,null);
            page.setAllRow(count);
            List<TaskCompleteVo> taskCompletes = taskCompleteDao.selectTaskCompletePage(taskName,status,studentName,null,page.getStart(),page.getSize());
            for (TaskCompleteVo taskComplete : taskCompletes) {
                User user = userDao.selectByPrimaryKey(taskComplete.getStudentId());
                Student studentByIdCard = studentDao.getStudentByIdCard(user.getIdCard());
                taskComplete.setUserName(studentByIdCard.getStudentName());
            }
            return ResponseResult.success(taskCompletes);
        }

    }

    /**
     * 查询所有
     *
     * @return
     */
    @Override
    public ResponseResult queryAll() {
        List<Task> plans = taskDao.selectAll();
        return ResponseResult.success(plans);
    }

    /**
     * 查询单个
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult getOne(Integer id) {
        Task task = taskDao.selectByPrimaryKey(id);
        TaskVo taskVo = new TaskVo();
        taskVo.setId(task.getId());
        taskVo.setTaskName(task.getTaskName());
        taskVo.setTaskDesc(task.getTaskDesc());
        taskVo.setStartTime(task.getStartTime());
        taskVo.setEndTime(task.getEndTime());
        taskVo.setJobId(task.getJobId());
        taskVo.setAttach(task.getAttach());
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        TaskComplete taskComplete = taskCompleteDao.selectByTaskIdAndStudent(id, userinfo.getId());
        if (taskComplete != null){
            taskVo.setCompleteDesc(taskComplete.getCompleteDesc());
        }

        return ResponseResult.success(taskVo);
    }

    @Override
    public ResponseResult getTaskComplete(Integer id) {
        TaskComplete taskComplete = taskCompleteDao.selectByPrimaryKey(id);
        TaskCompleteVo taskCompleteVo = new TaskCompleteVo();
        Task task = taskDao.selectByPrimaryKey(taskComplete.getTaskId());
        taskCompleteVo.setTaskName(task.getTaskName());
        taskCompleteVo.setCompleteDesc(taskComplete.getCompleteDesc());
        taskCompleteVo.setId(task.getId());
        taskCompleteVo.setEvaluate(taskComplete.getEvaluate());
        taskCompleteVo.setScore(taskComplete.getScore());
        taskCompleteVo.setTaskDesc(task.getTaskDesc());
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        taskCompleteVo.setUserType(userinfo.getUserType());
        return ResponseResult.success(taskCompleteVo);
    }

    /**
     * 保存数据
     *
     * @param plan
     * @return
     */
    @Override
    public ResponseResult save(Task plan) {
        if (plan.getId() == null){
            User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
            plan.setCreateTime(new Date());
            plan.setCreator(userinfo.getId());
            plan.setUpdateTime(new Date());
            plan.setUpdater(userinfo.getId());
            int insert = taskDao.insert(plan);
            if (insert<=0){
                return ResponseResult.fail();
            }
            List<JobStudent> jobStudents = jobStudentDao.selectByJob(plan.getJobId());
            for (JobStudent jobStudent : jobStudents) {
                TaskComplete taskComplete = new TaskComplete();
                taskComplete.setTaskId(plan.getId());
                taskComplete.setStudentId(jobStudent.getStudentId());
                taskComplete.setStatus(0);
                taskCompleteDao.insert(taskComplete);
            }
            return ResponseResult.success();
        }else{
            User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
            plan.setUpdateTime(new Date());
            plan.setUpdater(userinfo.getId());
            int updateByPrimaryKey = taskDao.updateByPrimaryKey(plan);
            if (updateByPrimaryKey<=0){
                return ResponseResult.fail();
            }
            return ResponseResult.success();
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        int i = taskDao.deleteByPrimaryKey(id);
        if (i<=0){
            return ResponseResult.fail();
        }
        return ResponseResult.success();
    }

    @Override
    public ResponseResult getMyTask(String status, Page page) {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        Integer count = taskDao.countMyTaskByCondition(status,userinfo.getId());
        page.setAllRow(count);
        List<TaskVo> planList  = taskDao.selectMyTaskByCondition(status,userinfo.getId(),page.getStart(), page.getSize());
        for (TaskVo taskVo : planList) {
            taskVo.setJobStr(jobDao.selectByPrimaryKey(taskVo.getJobId()).getJobName());
            taskVo.setUserName(userDao.selectByPrimaryKey(taskVo.getUpdater()).getUserName());
            taskVo.setUpdateTime(taskVo.getUpdateTime());
        }
        return ResponseResult.success(planList);
    }

    @Override
    public ResponseResult completeTask(Integer id, String completeDesc) {
        User userinfo = (User) SecurityUtils.getSubject().getPrincipal();
        taskCompleteDao.completeTask(id,userinfo.getId(),completeDesc);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult evaluate(Integer id, String evaluate, String score) {
        taskCompleteDao.evaluate(id,evaluate,score);
        return ResponseResult.success();
    }
}
